package com.sfac.modules.test.service.serviceimpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sfac.modules.common.vo.Result;
import com.sfac.modules.common.vo.Search;
import com.sfac.modules.common.vo.StatusCode;
import com.sfac.modules.test.mapper.CityMapper;
import com.sfac.modules.test.pojo.City;
import com.sfac.modules.test.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Optional;


@Service
public class CityServiceImpl implements CityService {
    @Autowired
    private CityMapper cityMapper;
//    @Autowired
//    private CityMapper2 cityMapper2;

    /**
     * 添加城市
     * @param city
     * @return
     */
    @Override
    public Result<City> insertCity(City city) {
        if (cityMapper.getCityByName(city.getCityName()) == null){
            city.setDateModified(new Date());
            city.setDateCreated(new Date());
            cityMapper.insertCity(city);// 使用mybatis
//            cityMapper2.insert(city);// 使用mybatisPlus
            return new Result<City>(StatusCode.SUCCESS,"成功",city);
        }
        return new Result<City>(StatusCode.ERR,"城市名重复!");
    }


    /**
     * 修改城市
     * @param city
     * @return
     */
    @Override
    @Transactional
    public Result<City> updateCity(City city) {
        if (cityMapper.getCityByName(city.getCityName()) == null){
           return new Result<City>(StatusCode.ERR,"修改失败,信息不存在");
        }
        city.setDateModified(new Date());
        cityMapper.updateCity(city);
        return new Result<City>(StatusCode.SUCCESS,"修改成功!",city);
    }

    /**
     * 删除城市
     * @param cityId
     * @return
     */
    @Override
    public Result<City> deleteCity(int cityId) {
        return cityMapper.deleteCity(cityId) == 0? new Result<City>(StatusCode.ERR,"删除失败,信息不存在!"):new Result<City>(StatusCode.SUCCESS,"删除成功!");
    }

    /**
     * 查询城市根据id
     * @param cityId
     * @return
     */
    @Override
    public Result<City> selectCity(int cityId) {
        City city = cityMapper.selectCityByCityId(cityId);
        return city == null ? new Result<City>(StatusCode.ERR,"查询成功,但是没有这条信息!"):new Result<City>(StatusCode.SUCCESS,"查询成功",city);
    }

    /**
     * 查询所有城市
     * @return
     */
    @Override
    public Result<City> selectAllCitys() {
        List<City> citys = cityMapper.selectAllCitys();
        return citys == null?new Result<City>(StatusCode.ERR,"查询失败"):new Result<City>(StatusCode.SUCCESS,"查询成功",citys);
    }

    /**
     * 根据城市名称查询
     * @param cityName
     * @param localCityName
     * @return
     */
    @Override
    public Result getCityName(String cityName, String localCityName) {
        City city = cityMapper.getByCityName(cityName, localCityName);
        return new Result(StatusCode.SUCCESS,"查询成功!",city);
    }

    /**
     * 根据城市名称查询
     * @param countryId
     * @return
     */
    @Override
    public List<City> getCityByCountryName(String countryId) {
        // 如果查询结果为空则返回一个空的集合
        return Optional.ofNullable(cityMapper.selectCityByCountryId(countryId)).orElse(Collections.EMPTY_LIST);
    }

    /**
     * 分页查询
     * @param search
     * @return
     */
    @Override
    public PageInfo<City> getCitiesBySearch(Search search) {
        // 初始化search对象
        search.init();
        // 开启分页
        PageHelper.startPage(search.getCurrentPage(),search.getPageSize());
        // 查询返回
        return new PageInfo<>(Optional.ofNullable(cityMapper.getCitiesBySearch(search))
                .orElse(Collections.EMPTY_LIST));
    }
}
